<?php
declare (strict_types=1);

namespace app\api\controller;

use app\admin\controller\Theme;
use app\common\model\Area;
use app\common\model\Bank;
use app\common\model\Carousel;
use app\common\model\HotSearch;
use app\common\model\News;
use app\common\model\ProductCate;
use app\common\model\ProductType;
use app\common\model\User;
use app\common\model\UserCode;
use app\common\model\UserMenu;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\Response;

class Index extends Base
{
    public function index(): Response
    {
        $system = $this->system;
        $result = [
            'title' => $system['web_site_title'],
            'logo' => $system['web_site_logo'],
            'customer' => $system['customer'],
            'contact' => $system['contact'],
            'filing_number' => $system['filing_number']
        ];

        return Result::success('success', $result);
    }

    public function test(): Response
    {
        return Result::fail('访问无效');
    }

    public function theme(): Response
    {
        $theme = Theme::getTheme();
        $path = "/static/api/{$theme['version']}/{$theme['color']}/";
        $colorData = config('theme')[$theme['version']]['color'][$theme['color']];
        $result = [
            'color' => $colorData,
            'bg' => [
                //登录注册页面背景图
                'login' => "{$path}bg/login.png",
                //首页背景图
                'index' => "{$path}bg/index.png",
                //个人中心背景图
                'user' => "{$path}bg/user.png",
                //我的钱包背景图
                'account' => "{$path}bg/account.png",
                //我的订单背景图
                'my_order' => "{$path}bg/my_order.png",
                //我的订单详情背景图
                'my_order_info' => "{$path}bg/my_order_info.png",
                //充值背景图
                'remit' => "{$path}bg/remit.png",
                //邀请好友背景图
                'share' => "{$path}bg/share.png",
                //公告小图标
                'icon_news' => "{$path}bg/icon_news.png",
                //首页爆款左侧图
                'icon_bk_left' => "{$path}bg/icon_bk_left.png",
                //首页爆款右侧图
                'icon_bk_right' => "{$path}bg/icon_bk_right.png",
            ],
            'tabBar' => [
                'color' => '#222222',
                'selectedColor' => $colorData['themeColor'],
                'backgroundColor' => '#ffffff',
                'borderStyle' => 'white',
                'fontSize' => '24rpx',
                'iconWidth' => '57rpx',
                'height' => '114rpx',
                'list' => [
                    [
                        'iconPath' => "/static/tabbar/{$theme['color']}/home_w@2x.png",
                        'selectedIconPath' => "/static/tabbar/{$theme['color']}/home@2x.png",
                        'text' => '首页'
                    ],
                    [
                        'iconPath' => "/static/tabbar/{$theme['color']}/fenlei_w@2x.png",
                        'selectedIconPath' => "/static/tabbar/{$theme['color']}/fenlei@2x.png",
                        'text' => '分类'
                    ],
                    [
                        'iconPath' => "/static/tabbar/{$theme['color']}/gouwuc_w@2x.png",
                        'selectedIconPath' => "/static/tabbar/{$theme['color']}/gouwuc@2x.png",
                        'text' => '购物车'
                    ],
                    [
                        'iconPath' => "/static/tabbar/{$theme['color']}/wode_w@2x.png",
                        'selectedIconPath' => "/static/tabbar/{$theme['color']}/wode@2x.png",
                        'text' => '我的'
                    ]
                ]
            ]
        ];

        return Result::success('success', $result);
    }

    public function communal($type = 'register_msg'): Response
    {
        $title = [
            'close_msg' => '系统维护提示',
            'register_msg' => '注册协议',
            'privacy_msg' => '隐私协议',
            'company' => '关于我们'
        ];
        if (empty($title[$type])) {
            return Result::fail('传参有误');
        }

        return Result::success('success', handleRichText($this->system[$type]));
    }

    public function areaList($pid = 0, $level = 0): Response
    {
        return Result::success('success', Area::getArea($pid));
    }

    public function bankList(): Response
    {
        $list = Bank::where(['status' => 1])->order('sort desc')->column('title');

        return Result::success('success', $list);
    }

    public function carouselList($position = ''): Response
    {
        $map = ['status' => 1];
        if (!empty($position)) {
            $map['position'] = $position;
        }
        $list = Carousel::field('type,title,images,video,link')->where($map)
            ->order('sort desc')->select();

        return Result::success('success', $list);
    }

    public function searchList(): Response
    {
        $list = HotSearch::where(['status' => 1])->order('sort desc')->column('title');

        return Result::success('success', $list);
    }

    public function sendSms(): Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'UserCode');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

        return Result::fastReturn(UserCode::sendMsgCode($param['mobile'], $param['action']));
    }

    public function inquiryUser($code, $type = 'code'): Response
    {
        $user = User::where([$type => $code])->field('id,code,nickname,mobile,photo')->find();
        if (!$user) {
            return Result::fail('会员不存在');
        }

        return Result::success('success', $user);
    }

    public function info(): Response
    {
        $result['title'] = $this->system['web_site_title'];
        $result['logo'] = $this->system['web_site_logo'];
        $result['filing_number'] = $this->system['filing_number'];
        $result['search'] = HotSearch::where(['status' => 1])->order('sort desc')->column('title');
        $result['carousel'] = Carousel::field('type,title,images,video,link')->where(['status' => 1, 'position' => 1])
            ->order('sort desc')->select();
        $result['news'] = News::field('id,title')->where(['status' => 1])
            ->order('sort desc')->limit(3)->select();
        //$result['cate'] = ProductCate::getCate();

        $type = ProductType::field('id,icon,title,describe')->where(['status' => 1])
            ->order('sort desc')->append(['url'])->select();
        $result['cate'] = [];//$type;
        $result['type'] = $type;

        return Result::success('success', $result);
    }

    public function userMenu(): Response
    {
        $this->verifyToken();
        if (!empty($this->user)) {
            $menu = UserMenu::getDataList($this->system, $this->user);
        } else {
            $menu = UserMenu::getDataList($this->system, '');
        }

        return Result::success('success', $menu);
    }
}